import {app, Menu, Notification} from 'electron'
import {createMainWindow} from "../renderer/utils/windowUtil"
import {isOnline} from '../renderer/utils/networkUtil'
import {getProperty} from "../renderer/utils/config"

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
    global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow

app.on('ready', () => {
    mainWindow = createMainWindow()
    Menu.setApplicationMenu(null)
    /*initMenu(mainWindow);*/
    initAppEvent()
})

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})

app.on('activate', () => {
    if (mainWindow === null) {
        mainWindow = createMainWindow()
    }
})

function alertNotice(title, body) {
    new Notification({
        title: title,
        body: body
    }).show()
}

/**
 * 应用监听事件初始化
 */
function initAppEvent() {
    /**
     * 联网状态监控
     */

    !function () {
        debugger
        if (getProperty("enableIsOnline")) {
            console.log("开始检查网络状态")
            var _isOnline;
            isOnline().then(
                () => {
                    _isOnline = true
                },
                () => {
                    _isOnline = false
                    alertNotice("网络状态", "网络已断开")
                })

            setInterval(() => {
                    isOnline().then(
                        (online) => {
                            if (_isOnline != online) {
                                _isOnline = online
                                alertNotice("网络状态", online ? "网络已连接" : "网络已断开")
                            }
                        }
                    )
                },
                10*1000
            )
        }
    }()

}